package com.byt.framlib.base;

import android.annotation.SuppressLint;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import android.util.ArrayMap;
import android.view.View;
import android.view.ViewGroup;

import com.byt.framlib.commonutils.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("NewApi")
public class TabFragmentAdapter extends FragmentStatePagerAdapter {
    private FragmentManager fm = null;
    private ArrayList<BaseFragment> fragmentList = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    //记录是否是首次显示
    private ArrayMap<Integer, Boolean> isFirstVisbles = new ArrayMap<>();

    public TabFragmentAdapter(FragmentManager fm, ArrayList<BaseFragment> fragmentList) {
        super(fm);
        this.fm = fm;
        this.fragmentList = fragmentList;
        for (int i = 0; i < fragmentList.size(); i++) {
            isFirstVisbles.put(i, false);
        }
    }

    public TabFragmentAdapter(FragmentManager fm, ArrayList<BaseFragment> fragmentList, List<String> mTitles) {
        super(fm);
        this.fm = fm;
        this.mTitles = mTitles;
        this.fragmentList = fragmentList;
        for (int i = 0; i < fragmentList.size(); i++) {
            isFirstVisbles.put(i, false);
        }
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (!isFirstVisbles.get(position)) {
            /* 这里是首次调用 */
            Fragment frag = fragmentList.get(position);
            isFirstVisbles.put(position, true);// 设置为不是首次
            if (frag instanceof BaseFragment) ((BaseFragment) frag).initFristData();// 加载数据
        }
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = fragmentList.get(position);
        if (!fragment.isAdded()) { // 如果fragment还没有added
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(fragment, fragment.getClass().getSimpleName() + position);
            ft.commitAllowingStateLoss();
            /**
             * 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后
             * 会在进程的主线程中，用异步的方式来执行。 如果想要立即执行这个等待中的操作，就要调用这个方法（只能在主线程中调用）。
             * 要注意的是，所有的回调和相关的行为都会在这个调用中被执行完成，因此要仔细确认这个方法的调用位置。
             */
            fm.executePendingTransactions();
        }

        if (fragment.getView().getParent() == null) {
            container.addView(fragment.getView()); // 为viewpager增加布局
        }
        return fragment.getView();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return !CollectionUtils.isNullOrEmpty(mTitles) ? mTitles.get(position) : "";
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(fragmentList.get(position).getView());
        object = null;
    }
}